package com.sitecsys.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.sitecsys.config.Result;
import com.sitecsys.mapper.FTLUserMapper;
import com.sitecsys.mapper.TbUserMapper;
import com.sitecsys.pojo.FTLUser;
import com.sitecsys.pojo.Face;
import com.sitecsys.pojo.result.FaceResult;
import com.sitecsys.service.FTLUserService;
import com.sitecsys.service.TbUserService;
import com.sitecsys.service.annotation.FaceLog;
import com.sitecsys.utils.JwtUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;


@RestController
@RequestMapping("/api/FTLUser")
public class FTLUserController {
    @Resource
    private FTLUserService ftlUserService;

    @Resource
    private FTLUserMapper ftlUserMapper;
    @PostMapping("/login")
    public Result login(@RequestBody JSONObject jsonObj ){
        return ftlUserService.login(jsonObj);
    }

    @PostMapping("/register")
    public Result register(@RequestBody JSONObject jsonObj){
        return ftlUserService.register(jsonObj);
    }

    @PostMapping("/userInfo")
    public Result userInfo(HttpServletRequest request){
        return ftlUserService.getUserByPhone(request);
    }

    @PostMapping("/getUserByPhoneAndName")
    public Result getUserByPhoneAndName(@RequestBody JSONObject jsonObj){
        return ftlUserService.getUserByPhoneAndName(jsonObj);
    }

    @PostMapping("/CheckUser")
    public Result CheckUser(@RequestBody JSONObject jsonObj){
        return ftlUserService.CheckUserByPhone(jsonObj);
    }

    @PostMapping("/checkUserByFace")
    public Result checkUserByFace(@RequestBody JSONObject jsonObj){
        return ftlUserService.checkUserByFace(jsonObj);
    }

    @PostMapping("/updatePassWord")
    public Result updatePassWord(@RequestBody JSONObject jsonObj){
        return ftlUserService.updatePassword(jsonObj);
    }

    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody JSONObject jsonObj){
        return ftlUserService.updateUserById(jsonObj);
    }

    @PostMapping("/newPwdUpdate")
    public Result newPwdUpdate(@RequestBody JSONObject jsonObj, HttpServletRequest request) throws Exception {
        return ftlUserService.newPwdUpdate(jsonObj, request);
    }

    @PostMapping("/upload-image")
    public Result uploadImage(@RequestBody JSONObject jsonObj, HttpServletRequest request) throws Exception {
        return ftlUserService.uploadImage(jsonObj, request);
    }

    @PostMapping("/updateAvatar")
    public Result updateAvatar(@RequestBody JSONObject jsonObj, HttpServletRequest request) throws Exception {
        return ftlUserService.updateAvatar(jsonObj,request);
    }


    @PostMapping("/getUsers")
    public Result getUsers(@RequestBody JSONObject jsonObj, HttpServletRequest request) throws Exception {
        return ftlUserService.getUsers(jsonObj, request);
    }


    @PostMapping ("/AdvSearch")
    public Result AdvSearch(@RequestBody JSONObject jsonObj, HttpServletRequest request) throws ParseException {
        return ftlUserService.getUsersByAdvSearch(jsonObj, request);
    }

    @PostMapping("/userDetail")
    public Result userDetail(@RequestBody JSONObject jsonObj){
        return ftlUserService.getUserById(jsonObj);
    }

    @PostMapping("/updateUserR")
    public Result updateUserR(@RequestBody JSONObject jsonObj){
        return ftlUserService.updateUserById(jsonObj);
    }

    @PostMapping("/delById")
    public Result delById(@RequestBody JSONObject jsonObj){
        return ftlUserService.delById(jsonObj);
    }


    @PostMapping("/reSetPwd")
    public Result reSetPwd(@RequestBody JSONObject jsonObj){
        return ftlUserService.reSetPwd(jsonObj);
    }

    @PostMapping("/batchDel")
    public Result initFace(@RequestBody JSONObject jsonObj){
        return ftlUserService.batchDel(jsonObj);
    }

    @PostMapping("/batchResetStatus")
    public Result batchResetStatus(@RequestBody JSONObject jsonObj){
        return ftlUserService.batchResetStatus(jsonObj);
    }

    @PostMapping("/getAuth")
    public Result getAuth(@RequestBody JSONObject jsonObj){
        return ftlUserService.getAuth(jsonObj);
    }

    @PostMapping("/getAuths")
    public Result getAuths(){
        return ftlUserService.getAuths();
    }

    @PostMapping("/updateAuths")
    public Result updateAuths(@RequestBody JSONObject jsonObj) throws JsonProcessingException {
        return ftlUserService.updateAuth(jsonObj);
    }


//    private Future<?> taskFuture;
//
//    @Async
//    public void startTask() {
//        taskFuture = CompletableFuture.runAsync(() -> {
//            // 执行任务的代码
//        });
//    }
//
//    public void cancelTask() {
//
//    }
//
//    @Async
//    @GetMapping("/getBigData")
//    public Result getBigData(HttpServletRequest request){
//        List<String> data = new ArrayList<>();
////        List<FTLUser> data1 = new ArrayList<>();
//        if ("start".equals(request.getHeader("action"))) {
////            taskService.startTask();
//            taskFuture = CompletableFuture.runAsync(() -> {
//                // 执行任务的代码
//                for (int i = 0; i < 10000000; i++){
//                    data.add("id:"+ i);
//                    data.add("name:"+ i);
//                    data.add("age:"+ i);
//                    System.out.println("i:"+i);
//                }
//            });
//
//        } else if ("cancel".equals(request.getHeader("action"))) {
//            if (taskFuture != null) {
//                taskFuture.cancel(true);
//            }
////            taskService.cancelTask();
//        }
//
//        return Result.success(data);
//    }

}
